package com.example;

import com.example.dao.BookDao;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

//初始化BeanFactory
public class AppForBeanFactory {
    public static void main(String[] args) {
        /*//1.使用BeanFactory初始化IOC容器
        Resource resources = new ClassPathResource("applicationContext.xml");
        BeanFactory bf = new XmlBeanFactory(resources);
        //2.获取bean对象
        BookDao bookDao = bf.getBean(BookDao.class);
        bookDao.save();*/

        //BeanFactory加载配置文件时，不会立即创建bean对象
        Resource resources = new ClassPathResource("applicationContext.xml");
        BeanFactory bf = new XmlBeanFactory(resources);
        System.out.println("BeanFactory 初始化完成");
        //第一次获取bean对象时才创建
        BookDao bookDao = bf.getBean(BookDao.class);
        bookDao.save();
        System.out.println("==================");
        //ApplicationContext 启动时会加载所有单例 Bean
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("BeanFactory 初始化完成");
        //获取 Bean（此时它已经被创建）
        BookDao bookDao1 = (BookDao) ctx.getBean("bookDao");
        bookDao1.save();
    }
}
